import{createRouter,createWebHashHistory} from "vue-router";
import {useLoginStore} from '../store/useLoginStore'
const router = createRouter({
    history:createWebHashHistory(),
    routes:[
        {
            path:"/",
            component:()=>import("../views/home.vue"),
            meta: {
                requireAuth: true,
              },
        },
        {
            path:"/login",
            component:()=>import("../views/login.vue")
        },
        {
            path:"/register",
            component:()=>import("../views/register.vue")
        }
    ]
})
//全局前置路由守卫
router.beforeEach((to,from,next)=>{
    const isLogin = useLoginStore().isLogin;
    if (to.matched.some(record => record.meta.requireAuth)) {
        if (isLogin) {
          console.log("已登录")
          next();
        } else {
          console.log("未登录")
          next("/login");
        }
      } else {
        next();
      }
})
export default router;